package gk.example.adapter;

import gk.example.bean.User;
import gk.example.util.ImageLoadUtil;

import java.util.List;

import com.example.gkdate.R;

import de.hdodenhof.circleimageview.CircleImageView;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class AttenderListAdapter extends BaseAdapter {

	private Context mContext;
	private List<User> mUserList;

	public AttenderListAdapter(Context context, List<User> userList) {
		mContext = context;
		mUserList = userList;
	}

	@Override
	public int getCount() {
		return mUserList.size();
	}

	@Override
	public Object getItem(int position) {
		return position;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		AttenderHolder attenderHolder = null;
		if (convertView == null) {
			attenderHolder = new AttenderHolder();
			convertView = LayoutInflater.from(mContext).inflate(
					R.layout.item_attender_list, parent, false);
			attenderHolder.imageHead = (CircleImageView) convertView
					.findViewById(R.id.image_attender_head);
			attenderHolder.imageSex = (ImageView) convertView
					.findViewById(R.id.image_attender_sex);
			attenderHolder.tvName = (TextView) convertView
					.findViewById(R.id.tv_attender_name);
			convertView.setTag(attenderHolder);
		} else {
			attenderHolder = (AttenderHolder) convertView.getTag();
		}
		ImageLoadUtil.getInstance().setImageBackground(mContext, false,
				attenderHolder.imageHead,
				mUserList.get(position).getUserHead().getFileUrl(mContext),
				null, 32, 32);
		if (mUserList.get(position).getSex().equals("男")) {
			attenderHolder.imageSex
					.setBackgroundResource(R.drawable.ic_sex_male);
		} else {
			attenderHolder.imageSex
					.setBackgroundResource(R.drawable.ic_sex_female);
		}
		attenderHolder.tvName.setText(mUserList.get(position).getNickName());
		return convertView;
	}

}

class AttenderHolder {
	TextView tvName;
	CircleImageView imageHead;
	ImageView imageSex;
}
